package com.siemens.industrial.web.mvc.service.admin;

import com.siemens.industrial.model.admin.Metadata;
import com.siemens.industrial.utils.JsonToOtherFormat;
import com.siemens.industrial.utils.UUIDGenerator;
import com.siemens.industrial.web.mvc.dao.admin.MetadataDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/11.
 */
@Service
public class MetadataService {
    @Resource
    MetadataDao metadataDao;

    @Transactional
    public void batchSave(List<Map<String, Object>> data, String key) {
        Map<String, String> reasons = getAllReason(key);
        for (Map<String, Object> meta : data) {
            String reason = meta.get(key + "Reason").toString();
            if (reasons.containsKey(reason)) {
                this.recoverMetadata(reasons.get(reason),JsonToOtherFormat.buildJsonToString(meta));
            } else {
                Metadata metadata = new Metadata();
//                metadata.setMetaId(UUIDGenerator.getUUID());
                metadata.setMetaKey(key);
                metadata.setMetaDefinition(JsonToOtherFormat.buildJsonToString(meta));
                metadataDao.save(metadata);
            }
        }
    }

    public void batchSaveMT(List<Object> data, String key) {
        for (Object meta : data) {
                Metadata metadata = new Metadata();
//                metadata.setMetaId(UUIDGenerator.getUUID());
                metadata.setMetaKey(key);
                metadata.setMetaDefinition(JsonToOtherFormat.buildJsonToString(meta));
                metadataDao.save(metadata);

        }
    }

    public List<Metadata> getAllMetadataBykey(String key) {
        return metadataDao.findAllByMetaKey(key);
    }

    public List<Metadata> getAllBykey(String key) {
        return metadataDao.findAllByKey(key);
    }

    public List<Metadata> getAllMetadata() {
        return metadataDao.findAll();
    }

    public void deleteMetadata(String metadataId) {
        metadataDao.delete(metadataId);
    }

    public void recoverMetadata(String metadataId,String definition) {
        metadataDao.recover(metadataId,definition);
    }

    private Map<String, String> getAllReason(String key) {
        List<Metadata> metadataList = this.getAllBykey(key);
        Map<String, String> reasons = new HashMap<>();
        for (Metadata meta : metadataList) {
            Map<String, Object> metaDefinition = JsonToOtherFormat.buildJsonToMap(meta.getMetaDefinition());
            Object reason = metaDefinition.get(key + "Reason");
            if (reason != null && !reason.equals("")) {
                reasons.put(reason.toString(), meta.getMetaId());
            }
        }
        return reasons;
    }

    public Metadata getMetadataByMetaId(String metaId){
        return metadataDao.findOneByMetaId(metaId);
    }

    public void updateById(Object definition,String metaId){
        metadataDao.updateById(JsonToOtherFormat.buildJsonToString(definition),metaId);
    }
}
